package com.ruitu.health_2.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.health_2.R;
import com.ruitu.health_2.ui.activity.ProductDetailActivity;
import com.ruitu.health_2.ui.adapter.ModelArticleListAdapter;
import com.ruitu.health_2.ui.adapter.ModelVideoListAdapter;
import com.ruitu.health_2.ui.adapter.SerachProListAdapter;
import com.ruitu.health_2.ui.common.ArticleListActivity;
import com.ruitu.health_2.ui.common.ProductListActivity;
import com.ruitu.health_2.ui.common.VideoDetailActivity;
import com.ruitu.health_2.ui.common.VideoListActivity;
import com.ruitu.router_module.Router;
import com.ruitu.router_module.bean.article.Article;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.bean.product.Video;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// 健康师的推荐页面
public class HealManRecoFragment extends BaseFragment {

    private LinearLayout ll_2, ll_3, ll_4;// 标题加列表的布局
    private RecyclerView rcv_list_2, rcv_list_3, rcv_list_4;// 数据列表
    private LinearLayout ll_data;// 搜索出来的数据
    private TextView tv_empty;
    private TextView tv_more_2, tv_more_3, tv_more_4;

    private ModelVideoListAdapter videoAdapter;// 视频
    private ModelArticleListAdapter articleAdapter;// 文章
    private SerachProListAdapter proAdapter;// 商品

    // 请求得到的数据
    private List<Video> videoList = new ArrayList<>();
    private List<Article> articleList = new ArrayList<>();
    private List<ProHome> proList = new ArrayList<>();

    public static HealManRecoFragment newInstance() {
        Bundle args = new Bundle();
        HealManRecoFragment fragment = new HealManRecoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_heal_man_reco, container, false);

        ll_2 = v.findViewById(R.id.ll_2);
        ll_3 = v.findViewById(R.id.ll_3);
        ll_4 = v.findViewById(R.id.ll_4);
        rcv_list_2 = v.findViewById(R.id.rcv_list_2);
        rcv_list_3 = v.findViewById(R.id.rcv_list_3);
        rcv_list_4 = v.findViewById(R.id.rcv_list_4);
        ll_data = v.findViewById(R.id.ll_data);
        tv_empty = v.findViewById(R.id.tv_empty);
        tv_more_2 = v.findViewById(R.id.tv_more_2);
        tv_more_3 = v.findViewById(R.id.tv_more_3);
        tv_more_4 = v.findViewById(R.id.tv_more_4);

        // 视频
        GridLayoutManager manager2 = new GridLayoutManager(getActivity(), 2);
        videoAdapter = new ModelVideoListAdapter(getActivity());
        rcv_list_2.setLayoutManager(manager2);
        rcv_list_2.setAdapter(videoAdapter);
        for (int i = 0; i < 4; i++) {
            Video video = new Video();
            video.setVideo("http://vfx.mtime.cn/Video/2019/03/19/mp4/190319212559089721.mp4");
            video.setTitle("视频标题" + i);
            video.setArtName("健康师" + i);
            video.setDetail("视频介绍" + i);
            video.setImgUrl("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2764516313,1059302380&fm=11&gp=0.jpg");
            videoList.add(video);
        }
        videoAdapter.setDataList(videoList);

        // 文章
        LinearLayoutManager manager3 = new LinearLayoutManager(getActivity());
        articleAdapter = new ModelArticleListAdapter(getActivity());
        rcv_list_3.setLayoutManager(manager3);
        rcv_list_3.setAdapter(articleAdapter);
        for (int i = 0; i < 4; i++) {
            Article article = new Article();
            article.setTitle("文章标题" + i);
            article.setArtName("健康师" + i);
            article.setReserveOne("文章介绍" + i);
            article.setSmallImg("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1148119306,3637214782&fm=26&gp=0.jpg");
            articleList.add(article);
        }
        articleAdapter.setDataList(articleList);

        // 商品
        LinearLayoutManager manager4 = new LinearLayoutManager(getActivity());
        proAdapter = new SerachProListAdapter(getActivity());
        rcv_list_4.setLayoutManager(manager4);
        rcv_list_4.setAdapter(proAdapter);
        for (int i = 0; i < 4; i++) {
            ProHome pro = new ProHome();
            pro.setId("26");
            pro.setName("商品名称" + i);
            pro.setDescription("商品介绍" + i);
            pro.setPrice("" + i);
            pro.setPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583312030964&di=a3f513fbd8d37d6b72744a17c898f68f&imgtype=0&src=http%3A%2F%2F1814.img.pp.sohu.com.cn%2Fimages%2Fblog%2F2009%2F7%2F23%2F9%2F1%2F12352592527g213.jpg");
            proList.add(pro);
        }
        proAdapter.setDataList(proList);

        ll_data.setVisibility(View.GONE);
        tv_empty.setVisibility(View.VISIBLE);
        setListeners();
        setViews();

        return v;
    }

    private void setListeners() {
        setOnClickListener(tv_more_2, tv_more_3, tv_more_4);

        // 点击视频查看详情
        videoAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("videoId", videoList.get(position).getId());
                bundle.putString("videoUrl", videoList.get(position).getVideo());
                startActivity(VideoDetailActivity.class, bundle);
            }
        });
        // 点击查看文章详情
        articleAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", articleList.get(position).getId());
                try {
                    Router.startActivity(getActivity(), "com.ruitu.health_1.ui.ArticleDetailsActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        // 点击查看商品详情
        proAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", proList.get(position).getId());
                startActivity(ProductDetailActivity.class, bundle);
            }
        });
    }

    // 根据数据设置页面
    private void setViews() {
        if (isDataEmpty(videoList)) {
            ll_2.setVisibility(View.GONE);
            tv_more_2.setVisibility(View.GONE);
        } else {
            ll_2.setVisibility(View.VISIBLE);
            if (videoList.size() >= 3) {
                tv_more_2.setVisibility(View.VISIBLE);
            } else {
                tv_more_2.setVisibility(View.GONE);
            }
        }
        if (isDataEmpty(articleList)) {
            ll_3.setVisibility(View.GONE);
            tv_more_3.setVisibility(View.GONE);
        } else {
            ll_3.setVisibility(View.VISIBLE);
            if (articleList.size() >= 3) {
                tv_more_3.setVisibility(View.VISIBLE);
            } else {
                tv_more_3.setVisibility(View.GONE);
            }
        }
        if (isDataEmpty(proList)) {
            ll_4.setVisibility(View.GONE);
            tv_more_4.setVisibility(View.GONE);
        } else {
            ll_4.setVisibility(View.VISIBLE);
            if (proList.size() >= 3) {
                tv_more_4.setVisibility(View.VISIBLE);
            } else {
                tv_more_4.setVisibility(View.GONE);
            }
        }

        if (ll_2.getVisibility() == View.GONE && ll_3.getVisibility() == View.GONE && ll_4.getVisibility() == View.GONE) {
            tv_empty.setVisibility(View.VISIBLE);
            ll_data.setVisibility(View.GONE);
        } else {
            tv_empty.setVisibility(View.GONE);
            ll_data.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_more_2) {// 更多视频
            Bundle bundle = new Bundle();
            bundle.putString("name", "");
            startActivity(VideoListActivity.class, bundle);
        }
        if (v == tv_more_3) {// 更多文章
            Bundle bundle = new Bundle();
            bundle.putString("name", "");
            startActivity(ArticleListActivity.class, bundle);
        }
        if (v == tv_more_4) {// 更多商品
            Bundle bundle = new Bundle();
            bundle.putString("name", "");
            startActivity(ProductListActivity.class, bundle);
        }
    }
}
